package edu.byu.intex2.data.bo;

import edu.byu.intex2.data.DataException;
import edu.byu.intex2.data.dao.SaleDAO;
import edu.byu.intex2.data.dao.TradeInLineDAO;
import edu.byu.intex2.data.dao.UsedCarDAO;

/**
 * Tracks each used car traded in towards a sale
 * @author Brady
 */
public class TradeInLine extends BusinessObject {
    // Sale
    private String saleId;
    private Sale sale;

    // Used Car
    private String usedCarId;
    private UsedCar usedCar;

    // Trade In Line Attributes
    private double tradeInValue;
    private int mileage;

    /** Creates a new instance of BusinessObject */
    public TradeInLine(String id) {
        super(id);
    }//constructor


    /** Convenience method to call the correct DAO to save this business object */
    public void save() throws DataException {
        try {
            TradeInLineDAO.getInstance().save(this);
        } catch (DataException e) {
            e.printStackTrace();
        }
    }

    /**  @return the tradeInValue     */
    public double getTradeInValue() {
        return tradeInValue;
    }

    /** @param tradeInValue the tradeInValue to set     */
    public void setTradeInValue(double tradeInValue) {
        this.tradeInValue = tradeInValue;
        setDirty();
    }

    /** @return the mileage     */
    public int getMileage() {
        return mileage;
    }

    /** @param mileage the mileage to set     */
    public void setMileage(int mileage) {
        this.mileage = mileage;
        setDirty();
    }

    /** @return the saleId     */
    public String getSaleId() {
        return saleId;
    }

    /** @param saleId the saleId to set     */
    public void setSaleId(String saleId) {
        this.saleId = saleId;
        setDirty();
    }


    /** @return the sale     */
    public Sale getSale() throws Exception {
        if (sale == null) {
            sale = SaleDAO.getInstance().read(saleId);
        }
        return sale;
    }

    /** @param sale the sale to set    */
    public void setSale(Sale sale) {
        this.sale = sale;
        setDirty();
    }

    /** @return the usedCarId     */
    public String getUsedCarId() {
        if (usedCarId == null){
            usedCarId = usedCar.getId();
        }
        return usedCarId;
    }

    /** @param usedCarId the usedCarId to set     */
    public void setUsedCarId(String usedCarId) {
        if (usedCarId == null){
            usedCarId = usedCar.getId();
        }
        this.usedCarId = usedCarId;
        setDirty();
    }

    /** @return the usedCar     */
    public UsedCar getUsedCar() throws Exception {
        if (usedCar == null) {
            usedCar = UsedCarDAO.getInstance().read(usedCar.getId());
        }
        return usedCar;
    }

    /** @param usedCar the usedCar to set     */
    public void setUsedCar(UsedCar usedCar) {
        this.usedCar = usedCar;
        setDirty();
    }
}
